package com.resource.project.service.system.service.impl;

import com.resource.project.entity.system.MenuRole;
import com.resource.project.mapper.MenuRoleMapper;
import com.resource.project.service.system.service.IMenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author resource
 * @since 2020-03-14
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {

    @Autowired
    MenuRoleMapper menuRoleMapper;

    public int updateMenuRole(Long rid, Long[] mids) {
        menuRoleMapper.deleteById(rid);
        if (mids.length == 0) {
            return 0;
        }
        return menuRoleMapper.addMenu(rid, mids);
    }
    @Transactional
    public boolean updateMenuByRoleId(Long id, Long[] mids) {
        menuRoleMapper.deleteById(id);
        if (mids.length==0){
            return true;
        }
        List<MenuRole> list = Arrays.stream(mids).map(t -> new MenuRole(t, id)).collect(Collectors.toList());
        return saveBatch(list, list.size());
    }
}
